Public Class frmComputerCleanup

    Private _Cleaner As ICleaner

    Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        _Cleaner = ComputerCleaner.GetInstance

    End Sub

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Cursor.Current = Cursors.WaitCursor

        Dim dt As DataTable
        If Me.radDeletionCandidates.Checked Then
            dt = _Cleaner.getDeletionCandidate
        ElseIf Me.radDisableCandidates.Checked Then
            dt = _Cleaner.getDisableCandidates
        ElseIf Me.radStaleLogons.Checked Then
            dt = _Cleaner.getStaleItems
        Else
            Dim myAD As ADComputerHelper = ADComputerHelper.GetInstance
            dt = myAD.getTargetComputers
        End If

        Dim dv As DataView = New DataView(dt)
        dv.Sort = "cn"

        With DataGridView1
            .DataSource = dv
        End With

        lblRowCount.Text = dv.Count & " " & _Cleaner.ItemName

        Cursor.Current = Cursors.WaitCursor

    End Sub

    Private Sub btnAddToPendingDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToPendingDisable.Click

        'verify
        If MessageBox.Show("Are you sure you want to mark these " & DataGridView1.RowCount & " " & _Cleaner.ItemName & " as pending disable?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then

            Dim dv As DataView = DataGridView1.DataSource

            Dim dt As DataTable = dv.ToTable
            'loop through the data source... add each account to pending disable
            Dim dr As DataRow
            For Each dr In dt.Rows
                System.Diagnostics.Debug.WriteLine(dr("cn"))
                Dim lastLogonDate As Date
                If Not IsDBNull(dr("LastLogonDate")) Then
                    lastLogonDate = dr("LastLogonDate")
                End If

                _Cleaner.MarkAsPendingDisable(dr("cn"), lastLogonDate, dr("LastModifiedDate"))
            Next
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        MessageBox.Show("Deletions through this tool are not yet supported.  Please delete the " & _Cleaner.ItemName & " manually.")

    End Sub

    Private Sub btnDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisable.Click

        'verify
        If MessageBox.Show("Are you sure you want to disable " & DataGridView1.RowCount & " " & _Cleaner.ItemName & "?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then

            Dim dv As DataView = DataGridView1.DataSource

            Dim dt As DataTable = dv.ToTable
            'loop through the data source... disable each
            Dim dr As DataRow
            For Each dr In dt.Rows
                System.Diagnostics.Debug.WriteLine(dr("cn"))
                Dim lastLogonDate As Date
                If Not IsDBNull(dr("LastLogonDate")) Then
                    lastLogonDate = dr("LastLogonDate")
                End If

                _Cleaner.DisableItem(dr("cn"), lastLogonDate, dr("LastModifiedDate"))
            Next
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        System.Diagnostics.Debug.WriteLine(_Cleaner.ExcludedGroupsFilter)

    End Sub

End Class
